package cn.madog.utils;

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.*;

import java.util.*;

public class ExcelExportUtils {

    private static XSSFWorkbook createWorkBook(String[] title) {
        XSSFWorkbook xssfWorkbook = null;
        try {
            xssfWorkbook = new XSSFWorkbook();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("创建excel文件错误");
        }
        XSSFSheet xssfSheet = xssfWorkbook.createSheet("题");

        XSSFCellStyle xssfCellStyle = xssfWorkbook.createCellStyle();
        xssfCellStyle.setAlignment(HorizontalAlignment.CENTER);

        XSSFRow row = xssfSheet.createRow(0);
        for (int i = 0; i < title.length; i++) {
            XSSFCell cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(xssfCellStyle);
        }

        return xssfWorkbook;
    }

    public static XSSFWorkbook exportA3(HashMap<String, List<QuestionInfo>> questions) {

        String[] title = {"题干", "c1_问题", "c1_a", "c1_b", "c1_c", "c1_d", "c1_e", "c1_正确答案", "c1_解析", "c1_视频解析",
                "c2_问题", "c2_a", "c2_b", "c2_c", "c2_d", "c2_e", "c2_正确答案", "c2_解析", "c2_视频解析",
                "c3_问题", "c3_a", "c3_b", "c3_c", "c3_d", "c3_e", "c3_正确答案", "c3_解析", "c3_视频解析",
                "c4_问题", "c4_a", "c4_b", "c4_c", "c4_d", "c4_e", "c4_正确答案", "c4_解析", "c4_视频解析",
                "c5_问题", "c5_a", "c5_b", "c5_c", "c5_d", "c5_e", "c5_正确答案", "c5_解析", "c5_视频解析"
        };

        XSSFWorkbook xssfWorkbook = createWorkBook(title);
        XSSFSheet xssfSheet = xssfWorkbook.getSheet("题");

        int index = 0;

        Iterator<Map.Entry<String, List<QuestionInfo>>> iterator = questions.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<QuestionInfo>> next = iterator.next();

            XSSFRow row = xssfSheet.createRow(index + 1);
            row.createCell(0).setCellValue(next.getKey());

            List<QuestionInfo> value = next.getValue();
            int questionIndex = 0;
            for (int i = 0; i < value.size(); i++) {

                row.createCell(i + 1 + questionIndex).setCellValue(value.get(i).getQuestionMain());
                if (value.get(i).getQuestionOption() != null) {
                    row.createCell(i + 1 + questionIndex + 1).setCellValue(value.get(i).getQuestionOption().getOrDefault("A", ""));
                    row.createCell(i + 1 + questionIndex + 2).setCellValue(value.get(i).getQuestionOption().getOrDefault("B", ""));
                    row.createCell(i + 1 + questionIndex + 3).setCellValue(value.get(i).getQuestionOption().getOrDefault("C", ""));
                    row.createCell(i + 1 + questionIndex + 4).setCellValue(value.get(i).getQuestionOption().getOrDefault("D", ""));
                    row.createCell(i + 1 + questionIndex + 5).setCellValue(value.get(i).getQuestionOption().getOrDefault("E", ""));
                } else {
                    row.createCell(i + 1 + questionIndex + 1);
                    row.createCell(i + 1 + questionIndex + 2);
                    row.createCell(i + 1 + questionIndex + 3);
                    row.createCell(i + 1 + questionIndex + 4);
                    row.createCell(i + 1 + questionIndex + 5);
                }

                row.createCell(i + 1 + questionIndex + 6).setCellValue(value.get(i).getQuestionRightAnswer());
                row.createCell(i + 1 + questionIndex + 7).setCellValue(value.get(i).getQuestionAnalysis());
                questionIndex += 8;
            }

            index++;
        }

        return xssfWorkbook;
    }

    public static XSSFWorkbook export(ArrayList<QuestionInfo> questions) {

        String[] title = {"题干", "A", "B", "C", "D", "E", "正确答案", "解析", "视频解析"};

        XSSFWorkbook xssfWorkbook = createWorkBook(title);
        XSSFSheet xssfSheet = xssfWorkbook.getSheet("题");

        for (int i = 0; i < questions.size(); i++) {
            XSSFRow xssfRow = xssfSheet.createRow(i + 1);
            QuestionInfo question = questions.get(i);

            xssfRow.createCell(0).setCellValue(question.getQuestionMain());

            if (question.getQuestionOption() != null) {
                xssfRow.createCell(1).setCellValue(question.getQuestionOption().getOrDefault("A", ""));
                xssfRow.createCell(2).setCellValue(question.getQuestionOption().getOrDefault("B", ""));
                xssfRow.createCell(3).setCellValue(question.getQuestionOption().getOrDefault("C", ""));
                xssfRow.createCell(4).setCellValue(question.getQuestionOption().getOrDefault("D", ""));
                xssfRow.createCell(5).setCellValue(question.getQuestionOption().getOrDefault("E", ""));
            } else {
                xssfRow.createCell(1);
                xssfRow.createCell(2);
                xssfRow.createCell(3);
                xssfRow.createCell(4);
                xssfRow.createCell(5);
            }

            xssfRow.createCell(6).setCellValue(question.getQuestionRightAnswer());
            xssfRow.createCell(7).setCellValue(question.getQuestionAnalysis());
        }

        return xssfWorkbook;
    }
}
